package com.ocean.redflagtest.mvvm.viewmodels

import android.app.Application
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData

import com.ocean.redflagtest.R
import com.ocean.redflagtest.mvvm.models.entity.CircleModel
import com.ocean.redflagtest.mvvm.base.BaseViewModel
import com.ocean.redflagtest.mvvm.callback.OnRedFlagCallBack
import com.ocean.redflagtest.mvvm.models.entity.OriginalModel
import com.ocean.redflagtest.mvvm.models.repositorys.MainRepository

import java.util.ArrayList

/**
 * Created by ocean on 2018/8/2
 * Author :  ocean
 * Email  :  348686686@qq.com
 */
class MainViewModel(application: Application) : BaseViewModel<MainRepository>(application) {


    private var circleDatas: MutableLiveData<List<CircleModel>>? = null
    private var originalModels: List<OriginalModel>? = null

    init {
        respository = MainRepository(application)
        originalModels = respository.data

        circleDatas = MutableLiveData()
        respository.setmOnRedFlagCallBack { data ->
            originalModels = data
            if (originalModels != null && !originalModels!!.isEmpty()) {
                circleDatas!!.value = convert()
            }
        }
    }

    fun getCircleDatas(): MutableLiveData<List<CircleModel>>? {
        if (originalModels != null && !originalModels!!.isEmpty()) {
            circleDatas!!.setValue(convert())
        }
        return circleDatas
    }

    fun convert(): List<CircleModel> {
        val datas = ArrayList<CircleModel>()
        originalModels!!.forEach { originalModel ->
            val circleModel = CircleModel()
            circleModel.circleLeft = Integer.valueOf(originalModel.x)
            circleModel.circleTop = Integer.valueOf(originalModel.y)
            circleModel.circleRadius = Integer.valueOf(originalModel.size)
            circleModel.text = originalModel.name
            circleModel.imgResource = R.drawable.home_red
            datas.add(circleModel)
        }
        return datas
    }

}
